const url = require('url');
let server = () => {
    let G = {
        _get : {},
        _post : {} ,
        staticPath : 'static'
    };
    let app = function (req, res) {
        let pathname = url.parse(req.url).pathname;
        let method = req.method.toLowerCase() ;
        if (G['_'+method][pathname]) {

            if(method == 'get'){
                G['_'+method][pathname](req, res);
            } else {
                let postData = '' ;
                req.on('data',(chuck)=>{
                    postData += chuck
                })
                req.on('end',()=>{
                    console.log(postData)
                    res.end(postData)
                    G['_'+method][pathname](req, res);
                })
            }
            
        }
        else {
            res.writeHead(404, { 'Content-Type': 'text/html;charset="utf-8"' });
            res.end('page no found');
        }
    };
    app.get = function (str, cb) {
        G._get[str] = cb;
    };
    app.post = function (str, cb) {
        G._post[str] = cb;
    };
    return app
}

module.exports = server()
